#ifndef MAIN_WINDOW_H
#define MAIN_WINDOW_H

#include "utils/documentmanager.h"
#include "utils/perspectivemanager.h"

#include <settingsdialog.h>

#include <QtCore/QHash>
#include <QtGui/QMainWindow>

namespace swp
{
class ModelingPerspective;
class SimulationPerspective;
class StatisticsPerspective;

static const QString PROJECT_MENU = "PRO_MENU";
static const QString EDIT_MENU = "EDI_MENU";
static const QString VIEW_MENU = "VIE_MENU";
static const QString GO_MENU = "GO_MENU";
static const QString TOOLS_MENU = "TOO_MENU";
static const QString HELP_MENU = "HEL_MENU";
static const QString FINISHED = "FINISHED";

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    static MainWindow* getInstance();
    static void destroy();

    QMenu* getMenu(const QString& menuId) const;
    QToolBar* getToolBar(const QString& toolBarId) const;

    const core::IPerspective* getCurrentPerspective() const;
    const QList<core::IPerspective* > getPerspectives() const;

    void changePerspective(int index);

    void registerForSaveNotification(const core::IPerspective* perspective,
        QObject* listener);
    void unregisterForSaveNotification(const core::IPerspective* perspective,
        QObject* listener);
    void registerForCloseNotification(const core::IPerspective* perspective,
        QObject* listener);
    void unregisterForCloseNotification(const core::IPerspective* perspective,
        QObject* listener);

    void putToBag(const QString& key, const QVariant& value);
    QVariant bagValue(const QString& key) const;

protected:
    void closeEvent(QCloseEvent* event);

private:
    MainWindow();
    ~MainWindow();

    void init();

    void addMenus();
    void addToolBars();
    void createActions();
    void createMenus();
    void createToolbars();
    void createStatusBar();
    void createPerspectives();

    void connectSlots();

    void readSettings();
    void readPerspectiveSettings();
    void writeSettings();
    void writeSession();

private slots:
    void runValidation();
    void openOptions();

    void goToModeling();
    void goToSimulation();
    void goToStatistics();

    void sendSaveEvent();

    void showAbout();

    void handleEditorChange(const QSharedPointer<core::IEditor>& editor);

private:
    friend class ModelingPerspective;
    friend class PerspectiveManager;
    friend class SettingsHandler;

    QHash<QString, QVariant> m_bag;
    QHash<const core::IPerspective*, QList<QObject* > >
        m_registeredListenersForSave;
    QHash<const core::IPerspective*, QList<QObject* > >
        m_registeredListenersForClose;
    QHash<QString, QMenu* > m_menus;
    QHash<QString, QToolBar* > m_toolBars;
    DocumentManager* m_documentManager;
    PerspectiveManager* m_perspectiveManager;

    QTabWidget* m_perspectives;

    ModelingPerspective* m_modelingPerspective;
    SimulationPerspective* m_simulationPerspective;
    StatisticsPerspective* m_statisticsPerspective;

    gui::SettingsDialog* m_settingsDialog;

    QMenu* m_projectMenu;
    QMenu* m_editMenu;
    QMenu* m_viewMenu;
    QMenu* m_goMenu;
    QMenu* m_toolsMenu;
    QMenu* m_helpMenu;

    QToolBar* m_undoToolbar;

    // Undo toolbar
    QAction* m_undoAction;
    QAction* m_redoAction;

    // Edit menu
    QAction* m_undoMenuAction;
    QAction* m_redoMenuAction;

    // Tools menu
    QAction* m_validatorMenuAction;
    QAction* m_restoreSessionMenuAction;
    QAction* m_optionsMenuAction;

    // Go menu
    QAction* m_goToModelingMenuAction;
    QAction* m_goToSimulationMenuAction;
    QAction* m_goToStatisticsMenuAction;

    // Help menu
    QAction* m_aboutMenuAction;
};

} // namespace swp

#endif

